<?php
namespace Home\Controller;
use Think\Controller;
class FangjiaController extends Controller {
    private $token = null;
    private $name = "fjw_token";

    private $_fjw_username = "云途数据";
    private $_fjw_password = "123456";
    private $_fjw_appKey = "6343C3E708904478A02E868031CDBD133E85C8BA633F627E756BCF88800E7461";
    private $_fjw_token_expire = "6600";//1小时50分钟

    private $_fjw_url = array(
        //在访问房价网开放API前必须向房价网申请token。
        //注意：token的有效时间为2小时，过期后请调用授权接口重新获取
        'accessToken'=>'http://open.fangjia.com/accessToken',
        'propertyAvgPrice'=>'http://open.fangjia.com/property/avgPrice',//小区当前均价
        'propertyRent'=>'http://open.fangjia.com/property/rent',//小区租金接口
        'propertyEvaluate'=>'http://open.fangjia.com/property/evaluate',//对房子进行估价。
        'cityAvgPrice'=>'http://open.fangjia.com/city/avgPrice2',//查询指定城市的历史均价
        'districtAvgPrice'=>'http://open.fangjia.com/district/avgPrice',//获得区域均价走势数据
        'districtRent'=>'http://open.fangjia.com/district/rent',//获取住宅区域范围的平均租金走势数据。
        'blockAvgPrice'=>'http://open.fangjia.com/block/avgPrice',//获得板块均价走势数据
        );
    private $_message = null;

    //初始化
    public function __construct(){
        $this->getFjwToken();
    }

    public function __call($method,$args){

    }

    private function getFjwToken(){
        $filename = DATA_PATH.$this->name.".txt";
        //读取文件缓存
        if(file_exists($filename)){
            $file_contents = json_decode(file_get_contents($filename) );
            if($file_contents['expirse']>=time()){
                $this->token = $file_contents['token'];
            }
        }

        if(!$this->token){
            //请求房价网获取token地址
            $result = curl_get($this->_fjw_url['accessToken']."?username=".$this->_fjw_username."&password=".$this->_fjw_password."&appKey=".$this->_fjw_appKey);
            $fjw_result = null;
            if(isset($result['status'])==200){
                $fjw_result = json_decode($result['result'],true);
            }

            if(isset($fjw_result['result'])){
               $this->token = $fjw_result['result']['token'];
               $expirse = time()+ $this->_fjw_token_expire;
               $content = json_encode ( array('expirse'=>$expirse,'token'=>$this->token) );
               file_put_contents($filename, $content);
            } 

        }
        
    }

    //获得小区当前均价。
    public function propertyAvgPrice(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['propertyAvgPrice']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //获取小区租金走势数据。
    public function propertyRent(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['propertyRent']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //对房子进行估价。
    public function propertyEvaluate(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['propertyEvaluate']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //城市均价查询接口
    public function cityAvgPrice(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['cityAvgPrice']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //区域均价查询接口
    public function districtAvgPrice(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['districtAvgPrice']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //住宅区域平均租金接口
    public function districtRent(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['districtRent']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }

    //版块均价查询接口
    public function blockAvgPrice(){
        if(!$this->token) echo '{"code": 401,"msg": "没有权限访问对应的资源","result": ""}';

        $params = I('get.');
        $params_string = http_build_query($params);
        $url = $this->_fjw_url['blockAvgPrice']."?token=".$this->token;

        $result = curl_get($url."&".$params_string);
        
        if($result['status']==200){
            echo $result['result'];
        }else{
            echo '{"code":200,"msg":"ok","result":[]}';
        }
        exit;
    }



}